# import data_read
'''
data_2 = data_read.data_2
print("success")
'''
import numpy as np
import pandas as pd


interval_2 = [[590, 668],
              [668, 744],
              [744, 840],
              [840, 910],
              [910, 944],
              [944, 962],
              [962, 1055],
              [1055, 1140],
              [1140, 1185],
              [1185, 1280],
              [1280, 1340],
              [1340, 1430],
              [1430, 1485],
              [1485, 1590],
              [1590, 1735],
              [2840, 2865],
              [2865, 2995],
              [2995, 3650]
              ]

# 读取数据

data_2 = pd.read_excel(io='adjuncts/adjunct_2.xlsx', sheet_name="中红外", index_col=0)
# print("read success")

# 数据清洗  去除问题中检材编号  去除产地信息


def peak_interval(left, right, list_row, start):  # 计算区间峰值位置 参数按照检材编号,左闭右开

    lst = list_row[left - start: right - start]          # 切割传入函数数据   list_row是一个list
    # print("===== * =====")
    lst_new = np.ndarray.tolist(lst)             # 数据类型转换
    peak = max(lst_new)                           # 计算峰值
    if peak > 0:
        return [lst_new.index(peak) + left, peak]    # 返回峰值x, y轴坐标
    else:
        return [0, 0]                                # 出现较大干扰值  返回状态码


# =========预处理数据=======

def data_pretreatment(data, interval, start):

    table = []                                            # 二维列表 用来存放预处理数据
    for r in data.index:
        row = data.loc[r].values[0:]             # 按行读取 附件 数据
        if pd.isnull(row[0]):
            #  print("++++++++++++++", r)
            continue
        result_row = [row[0]]                                        # 存放该行数据处理结果
        for i in interval:                                     # 设i变量为区间库滚动游标
            t = peak_interval(i[0], i[1], row, start)                 # t接收返回的峰值 横纵坐标
            result_row.append(t[0])
            result_row.append(t[1])

        table.append(result_row)                          # 将该行数据存入表中
    return table


# print(data_2)

tables = data_pretreatment(data_2, interval_2, 551-1)   # 减去op列占的列数


l = [[6.0, 590, 0.408362, 668, 0.375945, 744, 0.309413, 908, 0.287446, 942, 0.307143, 960, 0.329, 1035, 0.760054, 1055,
     0.730104, 1151, 0.469264, 1243, 0.400337, 1311, 0.42824, 1384, 0.536992, 1430, 0.458399, 1569, 0.601398, 1654,
     0.796987, 2863, 0.342157, 2926, 0.439352, 3385, 0.76277]]  # 检验数据


def find_origin(data, nom):
    sampling = len(nom[0])
      # 二维列表 用来存放预处理数据
    lst_r = []
    for r in data.index:

        row = data.loc[r].values[0:]  # 按行读取 附件 数据
        if pd.isnull(row[0]):
            result = []
            for n in nom:
                sum_dif = 0
                t = []
                for k in range(2, sampling, 2):  # 步长为2
                    sum_dif += abs(row[k] - n[k])
                t.append(sum_dif)
                t.append(n[0])
                result.append(t)
            print("***", result)
        # temp = list([i[0] for i in result])
        # lst_r.append(result.index(min(temp)))
    return lst_r


def f_y(dif, lst):
    # print("***************" , len(lst))
    # difference_all = 200     # Magic numbers, Don't touch! :)
    difference_all = dif
    norms = [lst[0]]  # 标准对比库  先一第一行数据为标准
    # print("===", norms)
    # count = [1]              # 每种 药材数量
    count = [1]  # 每种 药材或产地 数量
    sampling = len(norms[0])  # 在每种检材上取样次数   len(interval)
    # print("success")

    for i in range(1, len(lst)):  # 设i变量为检材库对比游标   len(lst) == data_size
        # print(len(norms), sum(count))

        # print("检材:", i)
        for j in range(len(norms)):  # 设j变量为标准库对比游标
            # print("标准:", j)

            # print("123", norms)
            sum_dif = 0  # 用误差值累计求和   赋初值为0
            for k in range(2, sampling, 2):  # 步长为2 只计算x轴 差异
                sum_dif += abs(lst[i][k] - norms[j][k])  # 依次计算每种检材与每种标准样的差值 取绝对值
            if sum_dif <= difference_all:  # 如果误差不超过 规定值
                count[j] += 1  # 该种标准下 数量+1
                break
            j += 1
        if j >= len(norms):
            norms.append(lst[i])  # 超过误差 将该检材添加进标准库
            count.append(1)  # 数量库中新增该种样品 数量

    new_count = [i for i in count if i > 10]

    return norms


no = f_y(0.5969, tables)  # 标准标准库
print(no)
a = find_origin(data_2, no)
print(a)
